package com.example.money.test.HideToobar;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.example.money.test.R;
import com.example.money.test.TabLayout.ListFragment;
import com.example.money.test.TabLayout.ListFragmentAdapter;

import java.util.ArrayList;
import java.util.List;

public class CoordicatorActivity extends AppCompatActivity {
    private TabLayout mTabLayout;
    private ViewPager mPagers;
    private FloatingActionButton mFloating;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.coordinator_layout_one);

        android.support.v7.widget.Toolbar mToolbar = findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        init();
    }

    private void init() {
        mPagers = findViewById(R.id.pagers);
        mFloating = findViewById(R.id.floating);
        mFloating.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v,"响应成功",Snackbar.LENGTH_SHORT).show();
            }
        });
        initPagers();
    }

    @SuppressLint("WrongViewCast")
    private void initPagers() {
        mTabLayout = findViewById(R.id.tabs);

        List<String> tabs = new ArrayList<>();
        tabs = initTabs(tabs);

        for(int i = 0 ;i< tabs.size();i++){
            mTabLayout.addTab(mTabLayout.newTab().setText(tabs.get(i)));
        }

        List<Fragment> fls = new ArrayList<>();
        for(int i = 0;i<tabs.size();i++){
            fls.add(new ListFragment());
        }

        ListFragmentAdapter adapter = new ListFragmentAdapter(getSupportFragmentManager(),fls,tabs);
        //给ViewPager适配器
        mPagers.setAdapter(adapter);
        //将TabLayout 和ViewPager关联
        mTabLayout.setupWithViewPager(mPagers);

    }

    private List<String> initTabs(List<String> tabs) {
        tabs.add("动画");
        tabs.add("番剧");
        tabs.add("国创");
        tabs.add("舞蹈");
        tabs.add("音乐");
        tabs.add("科技");
        tabs.add("游戏");
        tabs.add("影视");
        tabs.add("放映厅");

        return tabs;
    }


}
